﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.XtraCharts;
using DevExpress.XtraCharts.Web;

namespace TireService.Web.UserControls.StatusOverview
{
    public partial class ucTireSetPatternOverview : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                TireServiceDB db = new TireServiceDB();

                BindTireSetPatternData(db, Convert.ToInt32(ddlTireType.SelectedItem.Value));
            }
        }

        /// <summary>
        /// Bind chart data
        /// </summary>
        /// <param name="db">Connection to database</param>
        /// <param name="TireTypeId">Type of tires</param>
        private void BindTireSetPatternData(TireServiceDB db, int TireTypeId)
        {
            List<TireSet> tiresets = db.TireSets.Where(p => p.TireSetStatusId == (int)Shared.TireSetStatus.OnStock & p.TireTypeId == TireTypeId).ToList();

            List<TirePatternCount> tirepatterncounts = new List<TirePatternCount>();

            foreach (TireSet tireset in tiresets)
            {
                string ContainerNumber = db.GetContainerNumberByCargoRoomId(Convert.ToInt32(tireset.ContainerCargoRoomId));

                // Handle LF
                if (tireset.LeftFrontTirePattern < 2)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = "0-2>";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.LeftFrontTirePattern >= 2 & tireset.LeftFrontTirePattern <= 4)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = "2-4";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.LeftFrontTirePattern > 4 & tireset.LeftFrontTirePattern <= 6)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = ">4-6";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.LeftFrontTirePattern > 6)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = ">6";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }

                // Handle RF
                if (tireset.RightFrontTirePattern < 2)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = "0-2>";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.RightFrontTirePattern >= 2 & tireset.RightFrontTirePattern <= 4)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = "2-4";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.RightFrontTirePattern > 4 & tireset.RightFrontTirePattern <= 6)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = ">4-6";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.RightFrontTirePattern > 6)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = ">6";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }

                // Handle LR
                if (tireset.LeftRearTirePattern < 2)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = "0-2>";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.LeftRearTirePattern >= 2 & tireset.LeftRearTirePattern <= 4)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = "2-4";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.LeftRearTirePattern > 4 & tireset.LeftRearTirePattern <= 6)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = ">4-6";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.LeftRearTirePattern > 6)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = ">6";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }

                // Handle RR
                if (tireset.RightRearTirePattern < 2)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = "0-2>";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.RightRearTirePattern >= 2 & tireset.RightRearTirePattern <= 4)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = "2-4";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.RightRearTirePattern > 4 & tireset.RightRearTirePattern <= 6)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = ">4-6";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }
                if (tireset.RightRearTirePattern > 6)
                {
                    TirePatternCount tirepatterncount = new TirePatternCount();
                    tirepatterncount.PatternScale = ">6";
                    tirepatterncount.TireCount = 1;
                    tirepatterncount.ContainerNumber = ContainerNumber;

                    tirepatterncounts.Add(tirepatterncount);
                }

                chartTireSetPattern.SeriesDataMember = "ContainerNumber";
                chartTireSetPattern.SeriesTemplate.ArgumentDataMember = "PatternScale";
                chartTireSetPattern.SeriesTemplate.ArgumentScaleType = DevExpress.XtraCharts.ScaleType.Qualitative;
                chartTireSetPattern.SeriesTemplate.ValueScaleType = DevExpress.XtraCharts.ScaleType.Numerical;
                chartTireSetPattern.SeriesTemplate.ValueDataMembers[0] = "TireCount";

                chartTireSetPattern.DataSource = tirepatterncounts;
                chartTireSetPattern.DataBind();
            }
        }

        public class TirePatternCount
        {
            public string ContainerNumber { get; set; }
            public string PatternScale { get; set; }
            public int TireCount { get; set; }
        }

        /// <summary>
        /// Callback function that refreshes Chart
        /// </summary>
        protected void chartTireSetPattern_CustomCallback(object sender, CustomCallbackEventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            BindTireSetPatternData(db, Convert.ToInt32(e.Parameter));
        }
    }
}